import Vue from 'vue'
import Router from 'vue-router'

import Homepage from '@/components/Homepage'
import Articles from '@/components/Articles'
import acurrentMap from '@/components/acurrentMap'
import Forget from '@/components/Forget'
import Login from '@/components/Login'
import Manager from '@/components/Manager'
import Register from '@/components/Register'
import User from '@/components/User'
import NotFound from '@/components/NotFound'

// 路由重复问题
const originalPush = Router.prototype.push

Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const routes = [
  {
    path: '/',
    name: 'homepage',
    component: Homepage
  },
  {
    path: '/articles',
    name: 'articles',
    component: Articles
  },
  {
    path: '/acurrentmap',
    name: 'acurrentmap',
    component: acurrentMap
  },
  {
    path: '/forget',
    name: 'forget',
    component: Forget
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path: '/manager',
    name: 'manager',
    component: Manager,
    meta: {
      requireAuth: true
    },
    redirect: '/mapm',
    children: [
      {
        path: '/mapm',
        name: 'mapm',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Map.vue')
      },
      {
        path: '/houseaudit',
        name: 'houseaudit',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Audit/HouseAudit.vue')
      },
      {
        path: '/reissueaudit',
        name: 'reissueaudit',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Audit/ReissueAudit.vue')
      },
      {
        path: '/housetype',
        name: 'housetype',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Area/HouseType.vue')
      },
      {
        path: '/district',
        name: 'district',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Area/District.vue')
      },
      {
        path: '/street',
        name: 'street',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Area/Street.vue')
      },
      {
        path: '/usermanage',
        name: 'usermanage',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Role/UserManage.vue')
      },
      {
        path: '/administrator',
        name: 'administrator',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Role/Administrator.vue')
      },
      {
        path: '/operationalLogs',
        name: 'operationalLogs',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Log/OperationalLogs.vue')
      },
      {
        path: '/loginlogs',
        name: 'loginlogs',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Log/LoginLogs.vue')
      },
      {
        path: '/statistical',
        name: 'statistical',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Statistical.vue')
      },
      {
        path: '/policymanage',
        name: 'policymanage',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Home/PolicyManage.vue')
      },
      {
        path: '/announcement',
        name: 'announcement',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Home/Announcement.vue')
      },
      {
        path: '/newsmanage',
        name: 'newsmanage',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Home/NewsManage.vue')
      },
      {
        path: '/permissionlist',
        name: 'permissionlist',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Power/PermissionList.vue')
      },
      {
        path: '/roleslist',
        name: 'roleslist',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/Manager/Power/RolesList.vue')
      }
    ]
  },
  {
    path: '/user',
    name: 'user',
    component: User,
    meta: {
      requireAuth: true
    },
    redirect: '/mapu',
    children: [
      {
        path: '/mapu',
        name: 'mapu',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/User/Map.vue')
      },
      {
        path: '/applicate',
        name: 'applicate',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/User/Applicate.vue')
      },
      {
        path: '/progress',
        name: 'progress',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/User/Progress.vue')
      },
      {
        path: '/personal',
        name: 'personal',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/User/Personal.vue')
      },
      {
        path: '/reissue',
        name: 'reissue',
        meta: {
          requireAuth: true
        },
        component: () => import('../others/User/Reissue.vue')
      }
    ]
  },
  {
    path: '*',
    component: NotFound
  }
]

const router = new Router({
  routes,
  mode: 'history',
  linkActiveClass: 'active'
})
// 路由拦截
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (sessionStorage.getItem('token')) {
      next()
    } else {
      next({
        path: '/login'
      })
    }
  } else {
    next()
  }
})
// 将router对象存入到Vue实例
export default router
